﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using BusinessLogicLayer;
using System.Web;
using EntityDataModel;
using LiveCPEWebService.Entity;
using System.Collections.Generic;

namespace LiveCPEWebService
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {

        private String userName
        {
            get { return (string)HttpContext.Current.Session["login"] ; }
            set { HttpContext.Current.Session["login"] = value; }
        }
        
        [OperationContract]
        public bool DoLogin(String pUsername, String pPassword)
        {
            // Vérification 
            try
            {
                // Vérification login / pass
                if (UserBLL.DoLogin(pUsername, pPassword))
                {
                    // Ajout à la session
                    userName = pUsername;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }
        [OperationContract]
        public void DoDisconnect()
        {
            userName = null;
        }
        // Récupération utilisateur courant
        [OperationContract]
        public UserWS GetUser()
        {
            if (userName != null)
            {
                UserWS usr = new UserWS();
                usr.Generate(UserBLL.GetUserByUserName(userName));
                usr.Email = UserBLL.GetEmail(userName);
                return usr;
            }
            else
            {
                return null;
            }
        }
        [OperationContract]
        public byte[] GetUserPicture()
        {
            if (userName != null)
            {
                return UserBLL.GetPictureForUser(userName);
            }
            else
            {
                return null;
            }
        }
        [OperationContract]
        public byte[] GetUserPictureByUser(String pUserName)
        {
            if (pUserName != null)
            {
                return UserBLL.GetPictureForUser(pUserName);
            }
            else
            {
                return null;
            }
        }
         // Récupération utilisateur courant
        [OperationContract]
        public List<MessageWS> GetMessages()
        {
           List<Message> lst = WallBLL.GetAllPublicMessagesForUser(userName);
           List<MessageWS> lstNew = new List<MessageWS>();
           foreach(Message msg in lst){
                MessageWS msgWS = new MessageWS();
                msgWS.Generate(msg);
                lstNew.Add(msgWS);
           }
           return lstNew;
        }
        [OperationContract]
        public List<CommentWS> GetComments(String pMessageId)
        {
            List<Comment> lst = MessageBLL.GetAllCommentsForMessage(pMessageId);
            List<CommentWS> lstNew = new List<CommentWS>();
            foreach (Comment msg in lst)
            {
                CommentWS cmtWS = new CommentWS();
                cmtWS.Generate(msg);
                lstNew.Add(cmtWS);
            }
            return lstNew;
        }
        [OperationContract]
        public String CreateUser(UserWS pUser)
        {
            try
            {
                // Creation du user ASP
                UserBLL.CreateUserASP(pUser.UserName, pUser.Password, pUser.Email);
                // Creation du user Metier
                UserBLL.CreateUser(pUser.UserName, pUser.FirstName, pUser.LastName, pUser.Gender, pUser.DateOfBirth);
                return "Creation OK.";
            }
            catch (Exception e)
            {
                return e.InnerException.Message;
            }
        }
        [OperationContract]
        public String UpdateUser(UserWS pUser)
        {
            try
            {
                UserBLL.UpdateUser(pUser.UserName, pUser.FirstName, pUser.LastName, pUser.Gender, pUser.DateOfBirth, pUser.Email);
                return "Update OK.";
            }
            catch (Exception e)
            {
                return e.InnerException.Message;
            }
        }
        [OperationContract]
        public bool SendPublicMessage(String pDest, String pContent)
        {
            try
            {
                MessageBLL.SendPublicMessage(userName, pDest, pContent);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        [OperationContract]
        public bool SendPrivateMessage(String pDest, String pContent)
        {
            try
            {
                MessageBLL.SendPrivateMessage(userName, pDest, pContent);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        [OperationContract]
        public bool SendComment(String pMessID, String pContent)
        {
            try
            {
                MessageBLL.AddComment(userName, pMessID, pContent);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        [OperationContract]
        public bool LikeMessage(String pMessID)
        {
            try
            {
                MessageBLL.LikeMessage(userName, pMessID);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        [OperationContract]
        public bool LikeComment(String pCommID)
        {
            try
            {
                MessageBLL.LikeComment(userName, pCommID);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        // Friends
        [OperationContract]
        public List<UserWS> getFriends()
        {
            List<UserWS> lst = new List<UserWS>();
            foreach(User usr in FriendshipBLL.GetAllFriendsForUser(userName))
            {
                UserWS usrNew = new UserWS();
                usrNew.Generate(usr);
                usrNew.Email = UserBLL.GetEmail(usrNew.UserName);
                lst.Add(usrNew);
            }
            // retour de la liste
            return lst;
        }
        [OperationContract]
        public List<UserWS> getAllUsers()
        {
            List<UserWS> lst = new List<UserWS>();
            foreach (User usr in UserBLL.GetAllUsers())
            {
                UserWS usrNew = new UserWS();
                usrNew.Generate(usr);
                usrNew.Email = UserBLL.GetEmail(usrNew.UserName);
                lst.Add(usrNew);
            }
            // retour de la liste
            return lst;
        }
        [OperationContract]
        public String AddFriend(String pUserName)
        {
            try
            {
                FriendshipBLL.CreateFriendship(userName, pUserName);
                return "FriendShip request done.";
            }
            catch (Exception e)
            {
                return e.InnerException.Message;
            }
        }
        [OperationContract]
        public String AcceptFriend(String pMessageId)
        {
            try
            {
                FriendshipBLL.AcceptFriendship(pMessageId);
                return "Accept request done.";
            }
            catch (Exception e)
            {
                return e.InnerException.Message;
            }
        }
        [OperationContract]
        public String RejectFriend(String pMessageId)
        {
            try
            {
                FriendshipBLL.RejectFriendship(pMessageId);
                return "Accept request done.";
            }
            catch (Exception e)
            {
                return e.InnerException.Message;
            }
        }
        [OperationContract]
        public bool UpdateImage(String pUsername, byte[] tb)
        {
            try
            {
                UserBLL.UpdatePictureForUser(pUsername, tb);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
